import api from '../../api'
import axios from "axios"

const cart = {
  state: {
    openid: '',
    logoUrl: '',
    agentid: '',
    isloading: false,
    tabbarActive: 0,
    searchKey: [],
    baseInfo: {},
    addressId: 0
  },

  getters: {
    getOpenid: state => state.openid,
    getLogoUrl: state => state.logoUrl,
    getAgentid: state => state.agentid,
    getLoading: state => state.isloading,
    getTabbarActive: state => state.tabbarActive,
    getSearchKey: state => state.searchKey,
    getBaseInfo: state => state.baseInfo,
    getAddressId: state => state.addressId
  },

  mutations: {
    setOpenid (state, data) {
      state.openid = data
    },
    setLogoUrl (state, data) {
      state.logoUrl = data
    },
    setAgentid (state, data) {
      state.agentid = data
    },
    setLoading (state, data) {
      state.isloading = data
    },
    setTabbarActive (state, data) {
      state.tabbarActive = data
    },
    setSearchKey (state, data) {
      state.searchKey = data
    },
    setBaseInfo (state, data) {
      state.baseInfo = data
    },
    setAddressId (state, data) {
      state.addressId = data
    }
  },

  actions: {
    fetchOpenid ({ commit }, data) {
      commit('setOpenid', data)
    },
    fetchLogoUrl ({ commit }, data) {
      if(location.href.indexOf('qsls.gzqnyf.com') >= 0){
        commit('setLogoUrl', 'http://qsls.gzqnyf.com/Content/img/mls_logo.png')
      }else {
        commit('setLogoUrl', 'http://qsms.gzqnyf.com/Content/img/ms_logo.png')
      }
    },
    fetchAgentid ({ commit }, data) {
      commit('setAgentid', data)
    },
    fetchLoading ({ commit }, data) {
      commit('setLoading', data)
    },
    fetchTabbarActive ({ commit }, data) {
      commit('setTabbarActive', data)
    },
    fetchSearchKey ({ commit }) {
      if(!localStorage.getItem('searchKey')) {
        commit('setSearchKey', [])
      }else {
        commit('setSearchKey', localStorage.getItem('searchKey').split(','))
      }
      
    },
    fetchBaseInfo ({ commit }, data) {
      axios.get(api.agent.indexInfo)
        .then(res=>{
          commit('setBaseInfo', res.data.data)
        })
    },
    fetchAddressId ({ commit }, data) {
      commit('setAddressId', data)
    }
    
  }
}

export default cart
